// -*- mode: groovy -*-
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.
//
//This is a Jenkinsfile for the model backwards compatibility checker. The format and some functions have been picked up from the top-level Jenkinsfile.

mx_lib = 'lib/libmxnet.so, lib/libmxnet.a, lib/libtvm_runtime.so,lib/libtvmop.so, 3rdparty/dmlc-core/libdmlc.a, 3rdparty/tvm/nnvm/lib/libnnvm.a'

node('restricted-utility') {
  // Loading the utilities requires a node context unfortunately
  checkout scm
  utils = load('ci/Jenkinsfile_utils.groovy')
}
utils.assign_node_labels(utility: 'restricted-utility', linux_cpu: 'restricted-mxnetlinux-cpu')

utils.main_wrapper(
core_logic: {
  stage('MBCC Train'){
    node(NODE_LINUX_CPU) {
      ws('workspace/modelBackwardsCompat') {
        utils.init_git()
        // Train models on older versions
        utils.docker_run('ubuntu_nightly_cpu', 'nightly_model_backwards_compat_train', false)
        // upload files to S3 here outside of the docker environment
        sh "./tests/nightly/model_backwards_compatibility_check/upload_models_to_s3.sh"
      }
    }
  }

  stage('MXNet Build'){
    node(NODE_LINUX_CPU) {
      ws('workspace/build-cpu') {
        utils.init_git()
        utils.docker_run('ubuntu_cpu','build_ubuntu_cpu', false)
        utils.pack_lib('cpu', mx_lib)
      }
    }
  }

  stage('MBCC Inference'){
    node(NODE_LINUX_CPU) {
      ws('workspace/modelBackwardsCompat') {
        utils.unpack_and_init('cpu', mx_lib)
        // Perform inference on the latest version of MXNet
        utils.docker_run('ubuntu_nightly_cpu', 'nightly_model_backwards_compat_test', false)
      }
    }
  }
}
,
failure_handler: {
// Only send email if model backwards compat test failed
    if (currentBuild.result == "FAILURE") {
    	emailext body: 'Nightly tests for model backwards compatibity on MXNet branch : ${BRANCH_NAME} failed. Please view the build at ${BUILD_URL}', replyTo: '${EMAIL}', subject: '[MODEL BACKWARDS COMPATIBILITY TEST FAILED] build ${BUILD_NUMBER}', to: '${EMAIL}'
    }
}
)

